<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%--发送重新考试试卷--%>
<%
    JSONObject jsonData = new JSONObject();
    String id = Util.null2String( request.getParameter( "id" ) );
    String dtr = Util.null2String( request.getParameter( "dtr" ) );
    String curdate = DateUtil.getCurrentDate();
    RecordSet rs = new RecordSet();
    rs.executeUpdate("update uf_kstjtz_dt1 set sfyfs=0 where mainid=? and dtr=?", id,dtr);
    try {
        ResourceComInfo comInfo = new ResourceComInfo();
        rs.executeQuery( "select * from uf_kstjtz where id=?", id );
        rs.next();
        String lcck = Util.null2String( rs.getString( "lcck" ) );
        String cjr = Util.null2String( rs.getString( "cjr" ) );
        String cjrq = Util.null2String( rs.getString( "cjrq" ) );
        String sjmcwb = Util.null2String( rs.getString( "sjmcwb" ) );
        String ktszlx = Util.null2String( rs.getString( "ktszlx" ) );
        String yjr = Util.null2String( rs.getString( "yjr" ) );
        String kssc = Util.null2String( rs.getString( "kssc" ) );
        String kskssj  = DateUtil.getFullDate().substring( 0,16 );
        String zkjssj = Util.null2String( rs.getString( "zkjssj" ) );
        String zf = Util.null2String( rs.getString( "zf" ) );
        String jgfs = Util.null2String( rs.getString( "jgfs" ) );
        String cjksry = Util.null2String( rs.getString( "cjksry" ) );
        if (!"".equals( zkjssj ) &&hasThanCurDate( zkjssj + ":00")) {
            rs.executeQuery( "select * from uf_kstjtz_dt1 where mainid=" + id + " and dtr=" + dtr );
            rs.next();
            String mxid = Util.null2String( rs.getString( "id" ) );
            String username = comInfo.getLastname( dtr );
            String requestname = "在线考试答题-" + username + "-" + curdate;
            Map<String, String> requestMainMap = new HashMap<>();
            requestMainMap.put( "dtr", dtr );
            requestMainMap.put( "cjr", cjr );
            requestMainMap.put( "gs", comInfo.getSubCompanyID( dtr ) );
            requestMainMap.put( "bm", comInfo.getDepartmentID( dtr ) );
            requestMainMap.put( "cjrq", cjrq );
            requestMainMap.put( "sjmc", sjmcwb );
            requestMainMap.put( "ktszlx", ktszlx );
            if (!"".equals( yjr )) {
                requestMainMap.put( "yjr", yjr );
            }
            requestMainMap.put( "kssc", kssc );
            requestMainMap.put( "kskssj", kskssj );
            requestMainMap.put( "ksjssj", zkjssj );
            requestMainMap.put( "zf", zf );
            requestMainMap.put( "jgfs", jgfs );
            requestMainMap.put( "cjksry", cjksry );
            requestMainMap.put( "jmsjid", id );
            requestMainMap.put( "kslx", "2" );
            requestMainMap.put( "sjsplc", lcck );
            requestMainMap.put("kszt", "0");
            requestMainMap.put( "sfzk", "0" );
            String requestid = WorkflowCreateByRequestMap( dtr, "133", requestname, "1", requestMainMap );
            if (Util.getIntValue( requestid ) > 0) {
                rs.executeQuery( "select id from formtable_main_286 where requestid=?", requestid );
                rs.next();
                String mainid = Util.null2String( rs.getString( "id" ) );
                String modeid = getIdToRequestid( lcck, "uf_sjtz" );
                String[] strs = new String[]{"1", "2", "3", "4"};
                for (int j = 0; j < strs.length; j++) {
                    rs.executeQuery("select tm,tmwb,fz,tmzf,tmxxnr,ROW_NUMBER() over(order by px) px from uf_sjtz_dt" + strs[j] + " where mainid=? order by px", modeid);
                    while (rs.next()) {
                        String tm = Util.null2String( rs.getString( "tm" ) );
                        String fz = Util.null2String( rs.getString( "fz" ) );
                        String px = Util.null2String( rs.getString( "px" ) );
                        String tmwb = Util.null2String(rs.getString("tmwb"));
                        String tmxxnr = Util.null2String(rs.getString("tmxxnr"));
                        String tmzf = Util.null2String(rs.getString("tmzf"));
                        rs.executeUpdate( "insert into formtable_main_286_dt" + strs[j] + " (mainid,tm,fz,px,tmwb,tmzf,tmxxnr) values(?,?,?,?,?,?,?)", mainid, tm, fz, px,tmwb,tmzf,tmxxnr );
                    }
                }
                //触发后流程后，台账中（考试状态）改为未答卷，清空（开始答卷时间）、（答卷结束时间）、（答题时长）、（试卷得分），（是否重考）改为是；
                rs.executeUpdate("update uf_kstjtz_dt1 set kszt=0,ksdjsj='',sjtjsj='',dtscfz='',sjdf=0,sfzk=0,sfyfs=0,sjxq=? where mainid=? and id=?",requestid, id, mxid);
            }
            jsonData.put( "status", true );
            jsonData.put( "message", "在线考试流程触发成功" );
        } else {
            jsonData.put( "status", false );
            jsonData.put( "message", "重考截止时间未设置或已超过" );
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print( jsonData.toJSONString() );

%>

<%!
    BaseBean baseBean = new BaseBean();

    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        baseBean.writeLog( "-----创建流程传递的参数个数--------" );
        baseBean.writeLog( "creatorId:" + creatorId );
        baseBean.writeLog( "workflowId:" + workflowId );
        baseBean.writeLog( "requestName:" + requestName );
        baseBean.writeLog( "IsNextFlow:" + IsNextFlow );

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid( creatorId );//创建人Id
        requestInfo.setWorkflowid( workflowId );//工作流Id
        requestInfo.setDescription( requestName );//请求标题
        if ("0".equals( IsNextFlow )) {
            requestInfo.setIsNextFlow( IsNextFlow );
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName( Util.null2String( entry.getKey() ) );
            propertyArray[p].setValue( Util.null2String( entry.getValue() ) );
            baseBean.writeLog( "\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue() );
            p++;
        }
        baseBean.writeLog( "-----创建流程传递的参数个数p=" + p );
        mainTableInfo.setProperty( propertyArray );
        requestInfo.setMainTableInfo( mainTableInfo );
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest( requestInfo );//创建请求id
            } catch (Exception e) {
                baseBean.writeLog( "=======" + e.getMessage() );
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer( "\r\n-----xwd创建工作流记录日志开始" );
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append( "\r\n-----姓名:" + rscif.getLastname( rcif.getRequestCreater( requestid ) ) );
            sbf.append( "\r\n-----时间:" + rcif.getRequestCreateTime( requestid ) );
            sbf.append( "\r\n-----创建流程:" + wfcif.getWorkflowname( workflowId ) );
            sbf.append( "\r\n-----请求:" + rcif.getRequestname( requestid ) );
            sbf.append( "\r\n-----请求:" + requestid );
            sbf.append( "\r\n-----创建工作流记录日志结束" );
            baseBean.writeLog( sbf.toString() );
        } catch (Exception e) {
            baseBean.writeLog( "错误：" + e );
        }
        return requestid;
    }

    //requestid转ID
    public static String getIdToRequestid(String requestid, String tablename) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from " + tablename + " where lcck=?", requestid );
        rs.next();
        return Util.null2String( rs.getString( 1 ) );
    }
    /*
     * 参数日期是否大于当前日期
     */
    public static boolean hasThanCurDate(String date) {
        boolean flag = false;
        long ts1 = 0L;
        long ts2 = 0L;
        try {
            if(date.length()>10){
                SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
                Date d1 = sdf.parse( date );
                ts1 = d1.getTime();
                Date d2 = sdf.parse(DateUtil.getFullDate() );
                ts2 = d2.getTime();
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
                Date d1 = sdf.parse( date );
                ts1 = d1.getTime();
                Date d2 = sdf.parse( DateUtil.getCurrentDate() );
                ts2 = d2.getTime();
            }
            if((ts1 - ts2)>=0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

%>




